package com.agricultural.machine.decorator;

import com.agricultural.machine.common.IMachine;
import com.agricultural.machine.common.MachineState;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 日志记录装饰器类 - 装饰器模式
 */
public class LoggingMachineDecorator extends MachineDecorator {
    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    
    public LoggingMachineDecorator(IMachine decoratedMachine) {
        super(decoratedMachine);
    }
    
    @Override
    public void start() {
        log("启动");
        super.start();
    }
    
    @Override
    public void stop() {
        log("停止");
        super.stop();
    }
    
    @Override
    public void executeTask(String task) {
        log("执行任务: " + task);
        super.executeTask(task);
    }
    
    @Override
    public void setState(MachineState state) {
        log("状态变更: " + state.getDescription());
        super.setState(state);
    }
    
    @Override
    public void updatePosition(double x, double y) {
        log("位置更新: (" + x + ", " + y + ")");
        super.updatePosition(x, y);
    }
    
    @Override
    public void work() {
        log("工作");
        super.work();
    }
    
    @Override
    public void setEmergencyStop(boolean emergency) {
        log(emergency ? "紧急停止" : "解除紧急停止");
        super.setEmergencyStop(emergency);
    }
    
    /**
     * 记录日志
     */
    private void log(String operation) {
        String timestamp = LocalDateTime.now().format(formatter);
        String message = String.format("[%s] 农机(ID=%s, 类型=%s) - %s", 
                timestamp, getMachineId(), getType(), operation);
        System.out.println("日志: " + message);
    }
}










